package com.iplant.mes.controller.webSocket;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.BFCService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.serviceImpl.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * WebSocket消息测试类
 *
 * @author YouWang·Peng
 * 2024-1-24 19:53:55
 */
@RestController
@RequestMapping("/api/WebSocket")
public class WebSocketController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(WebSocketController.class);

    public WebSocketController() {
    }

    @Autowired
    BFCService mBFCService;

    /**
     * 给所有在线用户推送消息
     */
    @GetMapping("/SendMsgAll")
    public Object SendMsgAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            String wMsg = StringUtils.parseString(request.getParameter("Msg"));
            if (StringUtils.isEmpty(wMsg)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            WebSocketServer.sendMessageAll(wMsg);

            wResult = GetResult(RetCode.SERVER_CODE_SUC, "消息推送成功", null, null);
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 给指定用户推送
     */
    @GetMapping("/SendMsg")
    public Object SendMsg(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            String wMsg = StringUtils.parseString(request.getParameter("Msg"));
            String wUserId = StringUtils.parseString(request.getParameter("UserId"));
            if (StringUtils.isEmpty(wMsg) || StringUtils.isEmpty(wUserId)) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            boolean wStatus = WebSocketServer.sendInfo(wMsg, wUserId);
            if (wStatus) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "消息推送成功!", null, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "推送失败：用户不在线!", null, null);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 通过消息ID推送消息
     */
    @GetMapping("/SendMessageByID")
    public Object SendMessageByID(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wMessaegID = StringUtils.parseInt(request.getParameter("MessaegID"));
            if (wMessaegID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mBFCService.BFC_SendMessageByID(wLoginUser, wMessaegID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
